package org.lg.core;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import javax.servlet.Servlet;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.WebListener;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.Reader;
import java.util.Enumeration;

@WebListener
public class InitListener implements ServletContextListener {
    //容器初始化时候 执行的方法
    public void contextInitialized(ServletContextEvent context) {

//        ServletRegistration aDefault = context.getServletContext().getServletRegistration("default");
//        aDefault.addMapping("*.jpg");
//        aDefault.addMapping("*.png");
        System.out.println("-------------------");
        //从cotext中获取servletContext容器 得到controller包的实际文件夹路径
        String controllerRealPath = context.getServletContext().getRealPath("/WEB-INF/classes/org/lg/web/controller");
        String serviceRealPath = context.getServletContext().getRealPath("/WEB-INF/classes/org/lg/service/impl");
        String daoRealPath = context.getServletContext().getRealPath("/WEB-INF/classes/org/lg/dao/impl");
        String mapperRealPath = context.getServletContext().getRealPath("/WEB-INF/classes/org/lg/mapper");
        //System.out.println(controllerRealPath)
        try {
            InitMapping.initDataSourceAndMapperMap(mapperRealPath);
            InitMapping.initDao(daoRealPath);
            InitMapping.initService(serviceRealPath);
            InitMapping.initController(controllerRealPath);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void contextDestroyed(ServletContextEvent context) {

    }

    /**
     *
     */


}
